//
//  MantisRichImgModel.m
//  IMMantisSDK_Example
//
//  Created by 陈志亮 on 2021/4/2.
//  Copyright © 2021 chenzhiliang. All rights reserved.
//

#import "MantisRichImgModel.h"
#import "MantisAcademicCellData.h"


#define textViweWidth (IMSCREEN_WIDTH-leftSpaceMargin-rightSpaceMargin-20)

@implementation MantisRichImgModel
-(CGSize)contentSizefromMsg:(NSString*)msg{
    CGFloat height=0;
    // NSLog(@"初始化msg%@",msg);
    if (![msg containsString:@"additionMsg"]) {
//        msg = [msg stringByReplacingOccurrencesOfString:@"webp" withString:@"png"];
//        NSAttributedString *richTextStr =[self fromHtmlStr:msg];
//        CGRect rect = [[NSString alloc]in boundingRectWithSize:CGSizeMake(IMSCREEN_WIDTH-leftSpaceMargin-rightSpaceMargin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :KIMFontRegularSize(16)} context:nil];
//        CGSize Textsize = CGSizeMake(ceilf(rect.size.width)+1, ceilf(rect.size.height));
//        
//        CGFloat textWidth =Textsize.width;
        
        
        
        
        
    }else
    {
        NSDictionary*contentDic=self.historyModel.msg.mj_keyValues;
        MantisTextCellContentData*content=[MantisTextCellContentData mj_objectWithKeyValues:contentDic];
        NSInteger btnCount=content.content.count;
        NSInteger rowCount=btnCount/3+btnCount%3;
        
        CGFloat addHeight=adaptWidth750(52)*rowCount+(rowCount-1)*5;
        
        height+=addHeight;
        
    }
    return  CGSizeMake(textViweWidth, height);
}


//把后台返回的标签转为富文本
-(NSAttributedString *)richStrFrom:(NSString*)msg{
     msg = [msg stringByReplacingOccurrencesOfString:@"webp" withString:@"png"];
    NSAttributedString*newStr = [self fromHtmlStr:msg];
    
    return newStr;
}

//把html标签转成富文本
-(NSAttributedString*)fromHtmlStr:(NSString*)html{

    NSData *data = [html dataUsingEncoding:NSUnicodeStringEncoding];
    
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSAttributedString *str = [[NSAttributedString alloc]initWithData:data

                                                               options:options

                                                    documentAttributes:nil

                                                                 error:nil];
    NSMutableAttributedString*asstr = [[NSMutableAttributedString alloc]initWithAttributedString:str];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
       /** 行高 */
       paraStyle.lineSpacing = 6;
    NSDictionary *dic = @{NSFontAttributeName:KIMFontRegularSize(16), NSParagraphStyleAttributeName:paraStyle
                             };
    [asstr addAttributes:dic range:NSMakeRange(0, str.length)];
    
    

   return asstr;
}

-(CGFloat)getImgCountfrom:(NSString*)str{
    
    if (str.length>0) {
        OGNode *htmlData=[ObjectiveGumbo parseNodeWithString:str];
        NSString*htmlStr = htmlData.html;
        NSArray*textArr = [htmlStr componentsSeparatedByString:@"\n"];
        NSMutableArray*divImgArr=[NSMutableArray new];
        for (NSString*divStr in textArr) {
            if ([divStr containsString:@"<img"]) {//有图片的标签存到数组
                NSString*newStr = [self getImgSrcStr:divStr];
                divImgArr = [newStr componentsSeparatedByString:@" "].mutableCopy;
            }
        }
        return  divImgArr.count;
    }
    return 0;
}


//拿到src图片资源
-(NSString*)getImgSrcStr:(NSString*)orginalStr{
    NSArray*divImgArr = [orginalStr componentsSeparatedByString:@" "].mutableCopy;
    NSString*  newAllStr=@"";
    for (NSString*allStr in divImgArr) {
        if ([allStr containsString:@"src="]) {
             newAllStr = [@"<img " stringByAppendingFormat:@"%@ >",allStr];
            
        }
    }
    
    return newAllStr;
}



@end
